﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using pos.webserver;


namespace pos
{
   
    public partial class WebServiceForm : Form
    {
        HttpListenerController _controller = null;
      

        public WebServiceForm()
        {
            InitializeComponent();

           // MessageBox.Show(System.IO.Directory.GetCurrentDirectory());
          
        }

    
     

    

        private void btnStart_Click(object sender, EventArgs e)
        {
          /*  string[] prefixes = new string[] {
                "http://localhost:8081/", 
                "http://127.0.0.1:8081/",
                "http://192.168.1.5:8081/",
                "http://58.247.54.13:8081/",
                 };
            string vdir = "/";
            string pdir = System.IO.Directory.GetCurrentDirectory();

            _controller = new HttpListenerController(prefixes, vdir, pdir);
            _controller.Start();
            while (_controller.State == HttpListenerController.STATE._init) ;
            if (_controller.State == HttpListenerController.STATE._error)
            {
                lblMessage.Text = _controller.Msg;
                _controller = null;
               
            }
            else
                lblMessage.Text = string.Format("Listening on {0}", prefixes[0]);*/
            string[] prefixes = new string[] {
               "http://localhost:8081/", 
                "http://127.0.0.1:8081/",
                "http://192.168.1.5:8081/",
                "http://58.247.54.13:8081/",
                 };
            string vdir = "/";
           // string pdir = @"G:\Temp";
            string pdir = System.IO.Directory.GetCurrentDirectory();

            _controller = new HttpListenerController(prefixes, vdir, pdir, OnErrorMsg);
            _controller.Start();
            while (_controller.State == HttpListenerController.STATE._init) ;
            if (_controller.State == HttpListenerController.STATE._error)
            {
                lblMessage.Text = _controller.Msg;
                _controller = null;

            }
            else
                lblMessage.Text = string.Format("Listening on :\r\n{0} \r\n当前目录:{1}", prefixes[0], pdir);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_controller != null)
            {
                _controller.Stop();
                _controller = null;
            }
            
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (_controller != null)
                _controller.Stop();
            _controller = null;
            //   System.Diagnostics.Process.GetCurrentProcess().Kill();

        }

        private void WebServiceForm_Load(object sender, EventArgs e)
        {
            btnStart_Click(null, null);
        }
     
        public void OnErrorMsg(string msg)
        {

          //  lblMessage.Text=msg+"\r\n"+lblMessage.Text;
        }
        
    }
}
